from django.shortcuts import render
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin
from . import models,ser
from django.conf import settings
from django.core.cache import cache
from rest_framework.response import Response
# Create your views here.
#轮播图
class BannerView(GenericViewSet, ListModelMixin):
    queryset = models.Banner.objects.filter(is_delete=False,is_show=True).order_by('orders')[:settings.BANNER_COUNTER]
    serializer_class = ser.BannerSerializer
    def list(self, request, *args, **kwargs):
        banner_list = cache.get('banner_list')
        if not banner_list:
            print('走数据库拿轮播图')
            response = super().list(request, *args, **kwargs)
            #response获取的就是Response(serializer.data)数据，Response 返回的数据时是有url地址拼接的
            banner_list =response.data
            #加到缓存中
            cache.set('banner_list', response.data, 60*60*24)
        return Response(data=banner_list)









